package com.bobo.dao.message;


import com.bobo.dao.BaseDao;
import com.bobo.model.Message;
import com.bobo.model.Topic;
import com.bobo.model.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by bobo on 16-10-29.
 */
public class MessageDaoImpl extends BaseDao<Message> implements IMessageDao {
    @Override
    public Integer create(Message message) {

        String sql = "INSERT INTO t_message(content, user_id, topic_id, create_time)VALUES (?,?,?,now())";

        return create(sql,message.getContent(),message.getUser().getId(),message.getTopic().getId());
    }

    @Override
    public Message getById(Integer id) {
        String sql = "SELECT  tm.content,tm.status,tu.id,tu.name, tm.create_time, tt.id as topic_id, tt.title FROM  t_message tm\n" +
                "LEFT JOIN t_user tu on tm.user_id = tu.id\n" +
                "LEFT JOIN t_topic tt on tm.topic_id = tt.id\n" +
                "where tm.id = ?;";
        Message message = new Message();
        ResultSet rs = getObject(sql, id);
        try {
            while (rs.next()){
                message.setId(id);
                message.setContent(rs.getString("content"));
                message.setStatus(rs.getInt("status"));
                message.setCreateTime(rs.getTimestamp("create_time"));

                User user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));

                Topic topic = new Topic();
                topic.setId(rs.getInt("topic_id"));
                topic.setTitle(rs.getString("title"));

                message.setUser(user);

                message.setTopic(topic);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return message;
    }

    @Override
    public Boolean upd(Message message) {
        String sql = "UPDATE t_message SET  status=? WHERE id=?";

        //TODO error
        return update(sql, message.getStatus(),message.getId());
    }

    @Override
    public List<Message> getMessagesByTopic(Integer topicId, Integer current, Integer pageSize,Integer status) {
        String sql = "SELECT tm.id messageid,tm.content,tm.create_time," +
                " tu.id userid,tu.name " +
                "  FROM t_message tm,t_user tu " +
                " WHERE  tm.user_id=tu.id " +
                " AND tm.status=? " +
                "  AND tm.topic_id=? " +
                " ORDER BY tm.create_time DESC ";
        List<Message> messages = new ArrayList<>();
        ResultSet rs = pager(sql, current, pageSize, status,topicId);
        try {
            while(rs.next()){
                Message message = new Message();
                message.setId(rs.getInt("messageid"));
                message.setContent(rs.getString("content"));
                message.setCreateTime(rs.getTimestamp("create_time"));

                User user = new User();
                user.setId(rs.getInt("userid"));
                user.setName(rs.getString("name"));
                message.setUser(user);
                messages.add(message);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return messages;
    }

    @Override
    public List<Message> getMessagesByUser(Integer userId, Integer current, Integer pageSize, Integer status) {
        return null;
    }

    @Override
    public List<Message> getMessages(Integer current, Integer pageSize, Integer status) {
        String sql = "SELECT tm.id messageid,tm.content,tm.create_time,tu.id userid,tu.name " +
                "  FROM t_message tm,t_user tu " +
                " WHERE  tm.user_id=tu.id " +
                " AND tm.status=? " +
                " ORDER BY tm.create_time ";
        List<Message> messages = new ArrayList<>();
        ResultSet rs = pager(sql, current, pageSize,status);
        try{
            while(rs.next()){
                Message message = new Message();
                message.setId(rs.getInt("messageid"));
                message.setContent(rs.getString("content"));
                message.setCreateTime(rs.getTimestamp("create_time"));
                message.setStatus(status);

                User user = new User();
                user.setId(rs.getInt("userid"));
                user.setName(rs.getString("name"));
                message.setUser(user);
                messages.add(message);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return messages;
    }

    @Override
    public Integer getCount(Integer userId,Integer topicId,Integer status) {
        StringBuilder sql = new StringBuilder("SELECT count(*) from v_topic where status=?");
        Integer pageNum = 0;
        ResultSet rs;
        if (userId != null) {
            sql.append(" and user_id=?");
            rs = getObject(sql.toString(), status, userId);
        } else if (userId != null) {
            sql.append(" and topic_id=?");
            rs = getObject(sql.toString(), status, topicId);
        } else {
            rs = getObject(sql.toString(), status);
        }

        try {
            while (rs.next()) pageNum = rs.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pageNum;
    }
}
